package com.example.yichuangyun.mvvm.ui.activity.order.view;

import androidx.lifecycle.MutableLiveData;

import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.model.order.BusinessRecordModel;
import com.example.yichuangyun.model.order.FileInfoDataModel;
import com.example.yichuangyun.model.order.IncomePayDataModel;
import com.example.yichuangyun.model.order.InvoiceDataModel;
import com.example.yichuangyun.model.order.OrderFollowDataModel;
import com.example.yichuangyun.model.order.OrderListDataModel;
import com.example.yichuangyun.model.order.ServiceRecordModel;
import com.example.yichuangyun.model.order.VisitRecordsModel;
import com.example.yichuangyun.mvvm.di.retrofit.BaseModel;
import com.example.yichuangyun.mvvm.di.retrofit.BaseNetCallback;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.subscribers.ResourceSubscriber;

public class OrderDetailsViewModel extends OrderListViewModel {

    public MutableLiveData<OrderListDataModel.OrderRecordsModel> orderRecordsModelMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<List<ServiceRecordModel.ServiceModel>> serviceListMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<List<BusinessRecordModel.OrderBusinessModel>> businessListMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<OrderFollowDataModel> followMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<List<OrderFollowDataModel.OrderFollowModel>> visitListMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<List<FileInfoDataModel.FileInfoModel>> fileInfoListMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<List<InvoiceDataModel.InvoiceModel>> invoiceListMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<List<IncomePayDataModel.IncomePayModel>> incomePayListMutableLiveData = new MutableLiveData<>();

    public MutableLiveData<Integer> moreRequestCountMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<String> visitListErrorMutableLiveData = new MutableLiveData<>();

    private int moreRequestCount;

    /**
     * 订单详情
     */
    public void getOrderDetail(String id) {
        addSubscribe(mRetrofitHelper.getOrderDetail(id).subscribeWith(new BaseNetCallback<OrderListDataModel.OrderRecordsModel>() {

            @Override
            public void onSuccess(BaseModel<OrderListDataModel.OrderRecordsModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (baseModel.getData() != null) {
                        orderRecordsModelMutableLiveData.setValue(baseModel.getData());
                    }
                }
            }
        }));
    }

    /**
     * 服务列表
     */
    public void getOrderDetailsForService(String id) {
        addSubscribe(mRetrofitHelper.getOrderDetailsForService(id).subscribeWith(new BaseNetCallback<ServiceRecordModel>() {

            @Override
            public void onSuccess(BaseModel<ServiceRecordModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (baseModel.getData() != null) {
                        serviceListMutableLiveData.setValue(baseModel.getData().getRecords());
                    }
                }
            }
        }));
    }

    /**
     * 商机列表
     */
    public void getOrderDetailsForBusiness(String businessId) {
        Map<String, Object> map = new HashMap<>();
        map.put("chanceId", businessId);
        map.put("page", 1);
        map.put("pageSize", Constant.pageSize);
        addSubscribe(mRetrofitHelper.getOrderDetailsForBusiness(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<BusinessRecordModel>() {

            @Override
            public void onSuccess(BaseModel<BusinessRecordModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (baseModel.getData() != null) {
                        businessListMutableLiveData.setValue(baseModel.getData().getRecords());
                    }
                }
            }
        }));
    }

    /**
     * 订单和服务跟进列表
     */
    public void getOrderDetailsForOrderFollow(String id) {
        addSubscribe(mRetrofitHelper.getOrderDetailsForOrderFollow(id).subscribeWith(new BaseNetCallback<OrderFollowDataModel>() {

            @Override
            public void onSuccess(BaseModel<OrderFollowDataModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (baseModel.getData() != null) {
                        followMutableLiveData.setValue(baseModel.getData());
                    }
                } else {
                    mErrorData.setValue("error");
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                mErrorData.setValue("error");
            }
        }));
    }

    /**
     * 客户跟进列表
     */
    public void getOrderDetailsForVisit(String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", id);
        map.put("page", 1);
        map.put("pageSize", 50);
        addSubscribe(mRetrofitHelper.getOrderDetailsForVisit(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<VisitRecordsModel>() {

            @Override
            public void onSuccess(BaseModel<VisitRecordsModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (baseModel.getData() != null) {
                        visitListMutableLiveData.setValue(baseModel.getData().getRecords());
                    }
                } else {
                    visitListErrorMutableLiveData.setValue("error");
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                visitListErrorMutableLiveData.setValue("error");
            }
        }));
    }

    /**
     * 文件信息
     */
    public void getOrderDetailsForFileInfo(String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", id);
        map.put("page", 1);
        map.put("pageSize", 50);
        addSubscribe(mRetrofitHelper.getOrderDetailsForFileInfo(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<FileInfoDataModel>() {

            @Override
            public void onSuccess(BaseModel<FileInfoDataModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (baseModel.getData() != null) {
                        fileInfoListMutableLiveData.setValue(baseModel.getData().getRecords());
                    }
                }
                moreRequestCount++;
                moreRequestCountMutableLiveData.setValue(moreRequestCount);
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                moreRequestCount++;
                moreRequestCountMutableLiveData.setValue(moreRequestCount);
            }
        }));
    }

    /**
     * 发票记录
     */
    public void getOrderDetailsForInvoice(String id) {
        addSubscribe(mRetrofitHelper.getOrderDetailsForInvoice(id).subscribeWith(new BaseNetCallback<InvoiceDataModel>() {

            @Override
            public void onSuccess(BaseModel<InvoiceDataModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (baseModel.getData() != null) {
                        invoiceListMutableLiveData.setValue(baseModel.getData().getRecords());
                    }
                }
                moreRequestCount++;
                moreRequestCountMutableLiveData.setValue(moreRequestCount);
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                moreRequestCount++;
                moreRequestCountMutableLiveData.setValue(moreRequestCount);
            }
        }));
    }

    /**
     * 发票记录
     */
    public void getOrderDetailsForIncomePay(String id) {
        addSubscribe(mRetrofitHelper.getOrderDetailsForIncomePay(id).subscribeWith(new BaseNetCallback<IncomePayDataModel>() {

            @Override
            public void onSuccess(BaseModel<IncomePayDataModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (baseModel.getData() != null) {
                        incomePayListMutableLiveData.setValue(baseModel.getData().getRecords());
                    }
                }
                moreRequestCount++;
                moreRequestCountMutableLiveData.setValue(moreRequestCount);
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                moreRequestCount++;
                moreRequestCountMutableLiveData.setValue(moreRequestCount);
            }
        }));
    }
}
